package com.edu.entity;

import java.util.ArrayList;
import java.util.List;

public class Page<T> {
	private List<T> data;
	private int pageNum; //当前页数
	private int pageSize; //每页显示数量
	private int rowNum; //数据库查出来的数据总数
	private int rowPage;//总页数
	private ArrayList<Integer> showPages;//显示页面的数量，一次显示十页
	private int beginNum;
	private int endNum;
	private int showPagesNum;
	private boolean hasUp;
	private boolean hasNext;
	public int getEndNum() {
		int num = 0;
		try {
			num = this.showPages.get(this.showPages.size() - 1);
		}catch(IndexOutOfBoundsException e){
			return 0;
		}
		return num;
	}
	public int getBeginNum() {
		int num = 0;
		try {
			num = this.showPages.get(0);
		}catch(IndexOutOfBoundsException e){
			return 0;
		}
		return num;
	}
	public int showPagesNum(){
		return this.showPages.size();
	}
	public int getRowPage() {
		return rowPage;
	}
	public void setRowPage(int rowPage) {
		this.rowPage = rowPage;
	}
	public List<T> getData() {
		return data;
	}
	public void setData(List<T> data) {
		this.data = data;
	}
	public int getPageNum() {
		return pageNum;
	}
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getRowNum() {
		return rowNum;
	}
	public void setRowNum(int rowNum) {
		this.rowNum = rowNum;
	}
	public boolean isHasUp() {
		return hasUp;
	}
	public void setHasUp(boolean hasUp) {
		this.hasUp = hasUp;
	}
	public boolean isHasNext() {
		return hasNext;
	}
	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}
	
	@Override
	public String toString() {
		return "Page [data=" + data + ", pageNum=" + pageNum + ", pageSize=" + pageSize + ", rowNum=" + rowNum
				+ ", rowPage=" + rowPage + ", hasUp=" + hasUp + ", hasNext=" + hasNext +", showPages="+ showPages.size() +"]";
	}
	
	public Page (List<T> data,int pageNum,int pageSize,int rowNum) {
		this.data = data;
		this.pageNum = pageNum;
		this.pageSize = pageSize;
		this.rowNum = rowNum;
		this.showPages = new ArrayList();
		if(rowNum % pageSize == 0) {
			this.rowPage = rowNum / pageSize;
		}else {
			this.rowPage = (rowNum / pageSize) + 1;
		}
		if(pageNum <= 1) {
			this.hasUp = false;
		}else {
			this.hasUp = true;
		}
		if(pageNum >= rowPage) {
			this.hasNext = false;
		}else {
			this.hasNext = true;
		}
		if(pageNum <= 5) {
			System.out.println("前十页");
			if(rowPage < 10) {
				for(int i = 0;i<rowPage;i++) {
					this.showPages.add(i+1);
				}
			}else {
				for(int i = 0;i<10;i++) {
					this.showPages.add(i+1);
				}
			}
		}else if(pageNum > 5 && pageNum < rowPage - 6) {
			System.out.println("大于十页");
			for(int i = 0, k = pageNum - 4;i<10;i++,k++) {
				this.showPages.add(k);
			}
		}else if(pageNum >= rowPage - 6) {
			System.out.println("最后十页");
			for(int i = 0, k = rowPage - 9;i<10;i++,k++) {
				this.showPages.add(k);
			}
		}
	}
}
